﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SLogo.Compile
{
    /// <summary>
    /// 用户自定义过程
    /// </summary>
    class CustomProcess
    {
        /// <summary>
        /// 过程名
        /// </summary>
        public String Name { get; set; }
        /// <summary>
        /// 过程参数列表
        /// </summary>
        public List<Variable> Arguments { get; set; }
        /// <summary>
        /// 过程代码块
        /// </summary>
        public List<IntermediateCode> CodeBlock { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CustomProcess()
        {
            Name = "";
            Arguments = new List<Variable>();
            CodeBlock = new List<IntermediateCode>();
        }
        /// <summary>
        /// 带全部参数的构造函数
        /// </summary>
        /// <param name="name">过程名</param>
        /// <param name="arguments">过程参数列表</param>
        /// <param name="codeBlock">过程代码块</param>
        public CustomProcess(String name, List<Variable> arguments, List<IntermediateCode> codeBlock)
        {
            Name = name;
            Arguments = arguments;
            CodeBlock = codeBlock;
        }
        /// <summary>
        /// 执行过程时需要添加变量的值进来
        /// </summary>
        /// <param name="newArgs">实际变量的列表</param>
        /// <returns>若变量个数相同，则成功</returns>
        public bool UpdateArguments(List<Agument> newArgs)
        {
            if (Arguments.Count == newArgs.Count)//检查变量个数
            {
                for (int i = 0; i < Arguments.Count; i++)//依次赋值
                {
                    if (newArgs[i].TheType == Agument.ArgumentType.String)//如果是字符串
                    {
                        Arguments[i].TheType = Variable.VariableType.String;
                        Arguments[i].StringValue = newArgs[i].StringValue;
                    }
                    else if (newArgs[i].TheType == Agument.ArgumentType.Number)//如果是数值
                    {
                        Arguments[i].TheType = Variable.VariableType.Number;
                        Arguments[i].NumberValue = newArgs[i].NumberValue;
                    }
                    else if(newArgs[i].TheType == Agument.ArgumentType.Variable)//如果是变量
                    {
                        Arguments[i].NumberValue = newArgs[i].VariableValue.NumberValue;
                        Arguments[i].StringValue = newArgs[i].VariableValue.StringValue;
                        Arguments[i].TheType = newArgs[i].VariableValue.TheType;
                    }
                }
                return true;
            }
            return false;
        }
    }
}
